package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.cndragon.R;
import com.sskj.cndragon.presenter.TransferActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.APKVersionCodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


public class TransferActivity extends BaseActivity<TransferActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_phoenix_coin)
    TextView tvPhoenixCoin;
    @BindView(R.id.et_transfer_account)
    EditText etTransferAccount;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    private String fenghuangbi;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_transfer;
    }

    @Override
    public TransferActivityPresenter getPresenter() {
        return new TransferActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        fenghuangbi = getIntent().getStringExtra(Constans.FENGHUANGBI);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setRightButtonOnClickListener(view -> TransferRecordActivity.start(this));
    }

    @Override
    protected void initData() {
        super.initData();
        tvVersion.setText(App.INSTANCE.getString(R.string.bottom1) + APKVersionCodeUtils.getVerName(this));
        tvPhoenixCoin.setText(NumberUtil.keep2(fenghuangbi));
        ClickUtil.click(300, btnConfirm, new ClickUtil.Click() {
            @Override
            public void click() {
                if (isEmpty(etTransferAccount)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruhuazhuanshuliang));
                    return;
                }
                if (Float.valueOf(getEtext(etTransferAccount)) > Float.valueOf(fenghuangbi)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appFenghuangbishuliangbuzu));
                    return;
                }
                if (isEmpty(etPayPwd)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurujiaoyimima));
                    return;
                }

                mPresenter.huazhuan(SPUtil.get(SPConfig.ID, ""),
                        getEtext(etTransferAccount),
                        getEtext(etPayPwd));
            }
        });
        tvVersion.setText(App.INSTANCE.getString(R.string.bottom1) + APKVersionCodeUtils.getVerName(this));
    }

    public static void start(Context context, String fenghuabi) {
        Intent intent = new Intent(context, TransferActivity.class);
        intent.putExtra(Constans.FENGHUANGBI, fenghuabi);
        context.startActivity(intent);
    }

    public void transferSuccess() {
        etTransferAccount.getText().clear();
        etPayPwd.getText().clear();
        TransferRecordActivity.start(this);
    }
}
